// pages/mime/exportWeight/exportWeight.js

import storageUtil from './../../../utils/storageUtil'
import toastUtil from './../../../utils/toastUtil'


// 获取应用实例
const app = getApp()

/**
 * 导出体重数据页面
 */
Page({

  /**
   * 页面的初始数据
   */
  data: {

    // 系统平台
    system: app.globalData.system,
    // 导航栏高度
    navigationHeight: app.globalData.navigationHeight,
    // 统一背景颜色
    unifyBgColor: app.globalData.unifyBgColor,
    // 顶部导航栏高度
    topNavigationHeight: app.globalData.topNavigationHeight,
    // 主色调
    mainColor: app.globalData.mainColor,
    // 临时文件地址
    tempFileUrl: null

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },


  /**
   * 导出体重数据
   */
  exportWeight() {
    console.log('exportWeight..............')
    if(this.data.tempFileUrl){
      this.openExcel(this.data.tempFileUrl)
      return
    }
    wx.showLoading({
      title: '加载中'
    })
    wx.cloud.callFunction({
      name: "exportWeight",
      data: {
        openid: storageUtil.getStorage("openId")
      },
      complete: res => {
        console.log('exportWeight.....complete.........')
        console.log(res)

        // this.getFileUrl(res.result.fileID)
        let cloudFileId = res.result.fileID
        console.log('weight_diary_wx.cloud.downloadFile... start.....')
        wx.showLoading({
          title: '加载中'
        })
        wx.cloud.downloadFile({
          fileID: cloudFileId,
          success: res => {
            console.log('weight_diary_wx.cloud.downloadFile... success..... res : ', res)
            // get temp file path
            console.log(res.tempFilePath)
            let tempFilePath = res.tempFilePath
            this.setData({
              tempFileUrl: tempFilePath
            })
            this.openExcel(tempFilePath)

          },
          fail: err => {
            console.log('weight_diary_wx.cloud.downloadFile... success..... err : ', err)
            // handle error
            console.log(err)
          },
          complete: res => {
            console.log('weight_diary_wx.cloud.downloadFile... complete..... res : ', res)
            // handle error
            console.log(res)
            toastUtil.hideLoading()
          }
        })

        toastUtil.hideLoading()
      }
    })
  },


  /**
   * 打开excel
   */
  openExcel(tempFilePath){
    wx.showLoading({
      title: '加载中'
    })
    wx.openDocument({
      showMenu: true,
      filePath: tempFilePath,
      success: ress => {
        console.log('打开文档成功')
      },
      fail: res => {
        toastUtil.showToast('打开文档失败')
      },
      complete: res => {
        console.log('weight_diary_wx.cloud.downloadFile... complete..... res : ', res)
        // handle error
        console.log(res)
        toastUtil.hideLoading()
      }
    })
  }

})
